package com.vpclub.extend;

import com.vpclub.bean.Blue;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.stereotype.Component;

/**
 * @author : leimingchao
 * @date Date : 2021年06月27日 16:04
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {


    /**
     * BeanDefinitionRegistry:Bean定义信息的保存中心，以后BeanFactory就是按照
     *                        BeanDefinitionRegistry里面保存的每一个bean定义信息创建bean实例
     * @param registry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor....postProcessBeanDefinitionRegistry....");
        System.out.println("MyBeanDefinitionRegistryPostProcessor.....bean的数量：" + registry.getBeanDefinitionCount());
        // RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Blue.class);
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();
        registry.registerBeanDefinition("hello",beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor....postProcessBeanFactory....");
        int count = beanFactory.getBeanDefinitionCount();
        System.out.println("MyBeanDefinitionRegistryPostProcessor....bean的数量：" + count);
    }
}
